package com.visionork.android.tai.activity;

import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.AsyncTask;
import android.os.Bundle;

import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.visionork.android.tai.model.Program;
import com.visionork.android.tai.model.ProgramRepositoryBase;

public abstract class ProgramListActivityBase extends ListActivity {
	protected ProgramItemAdapter itemAdapter;

	public ProgramListActivityBase() {
	}

	protected class ProgramItemAdapter extends ArrayAdapter<Program> {
		public ProgramItemAdapter(Context context, int textViewResourceId,
				List<Program> objects) {
			super(context, textViewResourceId, objects);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.program_list_item, null);
			}

			Program prog = getItem(position);

			TextView tvTitle = (TextView) v.findViewById(R.id.title);
			tvTitle.setText(prog.getTitle());

			TextView tvMeta = (TextView) v.findViewById(R.id.meta);
			tvMeta.setText(prog.getMeta());

			ImageView ivFavorite = (ImageView) v
					.findViewById(R.id.img_favorite);
			if (prog.isFavorite()) {
				ivFavorite.setImageResource(R.drawable.favorite_item);
				ivFavorite.setVisibility(View.VISIBLE);
			} else {
				ivFavorite.setVisibility(View.INVISIBLE);
			}
			return v;
		}
	}

	private class LoadProgramTask extends AsyncTask<Void, Void, Void> {
		private boolean useCache;
		protected ProgressDialog pd;

		public LoadProgramTask(boolean useCache) {
			this.useCache = useCache;
		}

		@Override
		protected Void doInBackground(Void... params) {
			getRepository().getPrograms(useCache);
			if (!useCache || getRepository().getProgramsRef().size() == 0) {
				getRepository().getPrograms(false);
			}
			return null;
		}

		@Override
		protected void onPreExecute() {
			Resources res = getResources();
			pd = ProgressDialog.show(ProgramListActivityBase.this, null,
					res.getString(R.string.load), true, false);
		}

		@Override
		protected void onPostExecute(Void param) {
			itemAdapter.notifyDataSetChanged();
			pd.dismiss();
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		itemAdapter = new ProgramItemAdapter(ProgramListActivityBase.this,
				R.layout.program_list_item, getRepository().getProgramsRef());
		setListAdapter(itemAdapter);
		loadData(true);
		registerForContextMenu(getListView());
	}

	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		Program program = getRepository().getPrograms().get(position);
		Intent intent = new Intent(this, ProgramActivityImpl.class);
		
		intent.putExtra(ProgramActivityBase.KEY_URL, program.getUrlLink());		
		intent.putExtra(ProgramActivityBase.KEY_PROGRAMTYPE, program.getProgramType());
		
		startActivity(intent);
	}
	
	protected void loadData(boolean useCache) {
		LoadProgramTask task = new LoadProgramTask(useCache);
		task.execute(new Void[] {});
	}

	protected abstract ProgramRepositoryBase getRepository();
}
